package com.example.bootdemo.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.bootdemo.common.result.ResultBody;
import com.example.bootdemo.common.util.DateUtil;
import com.example.bootdemo.common.util.JsonConvertUtil;
import com.example.bootdemo.common.util.LunarCalendar;
import com.example.bootdemo.common.util.ReadExcelUtil;
import com.example.bootdemo.entity.dto.EmployeeEntryDto;
import com.example.bootdemo.service.ExcelImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("api/import/")
public class ExcelImportController {
    @Autowired
    private ExcelImportService excelImportService;

    //  表1
    @RequestMapping(value = "importEntryInfoExcel", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody importEntryInfoExcel(MultipartFile file) throws Exception {
        ResultBody resultBody = new ResultBody();
        Map<String, Object> map = new HashMap<>();
        List<ArrayList<String>> readExcel = new ArrayList<>();
        List<EmployeeEntryDto> sheet1List = new ArrayList<>();
//        List<EmployeeEntryDto> sheet2List = new ArrayList<>();
//        List<EmployeeEntryDto> sheet3List = new ArrayList<>();
//        List<EmployeeEntryDto> sheet4List = new ArrayList<>();
        try {
            //调用util方法拿到解析的数据集合
//            readExcel = new ReadExcelUtil().readExcel(file);
            map = new ReadExcelUtil().readExcel(file);
            Object sheet1 = map.get("sheet1");
//            Object sheet2 = map.get("sheet2");
//            Object sheet3 = map.get("sheet3");
//            Object sheet4 = map.get("sheet4");
            sheet1List = getSheet1List(sheet1);
//            sheet2List = getSheet2List(sheet2);
//            sheet3List = getSheet3List(sheet3);
//            sheet4List = getSheet4List(sheet4);
//            ExcelUtiles.importExcel(file,)
            List<List<EmployeeEntryDto>> lists=new ArrayList<>();
            lists.add(sheet1List);
//            lists.add(sheet2List);
//            lists.add(sheet3List);
//            lists.add(sheet4List);
            excelImportService.insertExcel(lists);
            resultBody.setMessage("导入成功");
            resultBody.setCode("1");
        } catch (IOException e) {
            e.printStackTrace();
            resultBody.setMessage("导入失败");
            resultBody.setCode("-1");
        }
        return resultBody;
    }



    public List<EmployeeEntryDto> getSheet1List(Object sheet1)throws Exception{
        List<EmployeeEntryDto> sheetList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String idCardBirthday="";
        String m="";
        String d="";
        String realbirthdayType="";
        String a = JsonConvertUtil.objectToJson(sheet1);
        JSONArray objects = JSONArray.parseArray(a);
        for (int i = 0; i < objects.size(); i++) {
            Object o = objects.get(i);
            String b = JsonConvertUtil.objectToJson(o);
            List<Map<String, Object>> listObjectSec = JSONArray.parseObject(b, List.class);
            int count = 0;
            //员工对象包含项目
            EmployeeEntryDto employeeEntryDto = new EmployeeEntryDto();
            //项目对象
//            Project projectExcelDto = new Project();
//            List<Project> projectExcelDtos = new ArrayList<>();
            for (Map<String, Object> mapList : listObjectSec) {
                if (0 == count) {
                    // 工号
                    employeeEntryDto.setJobNumber(mapList.get("value").toString());
                } else if (1 == count) {
                    //姓名
                    employeeEntryDto.setName(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setNameComment(mapList.get("remark").toString());
                    }
                } else if (2 == count) {
                    //部门,不进行设置
                    /*employeeEntryDto.setDepartment(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setDepartmentComment(mapList.get("remark").toString());
                    }*/
                } else if (3 == count) {
                    //岗位
                    employeeEntryDto.setJob(mapList.get("value").toString());
                    if (mapList.size()>1){
                        String str = mapList.get("remark").toString();
                        employeeEntryDto.setJobComment(str);
                        String subString = str.substring(0, str.indexOf("-"));
                        if(subString != null && !"".equals(subString)){
                            employeeEntryDto.setDepartmentId(Integer.parseInt(subString));
                        }
                    }
                } else if (4 == count) {
                    //入职日期
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        employeeEntryDto.setInTime(mapList.get("value").toString().equals("") ? null : sdf.parse(mapList.get("value").toString()));
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setInTimeComment(mapList.get("remark").toString());
                    }
                } else if (5 == count) {
                    //转正日期
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        employeeEntryDto.setEndProbationTime(mapList.get("value").toString().equals("") ? null : sdf.parse(mapList.get("value").toString()));
                    }

                    if (mapList.size()>1){
                        employeeEntryDto.setEndProbationTimeComment(mapList.get("remark").toString());
                    }
                } else if (6 == count) {
                    //合同截止期
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        employeeEntryDto.setEndContractDate(mapList.get("value").toString().equals("") ? null : sdf.parse(mapList.get("value").toString()));
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setEndContractDateComment(mapList.get("remark").toString());
                    }
                } else if (7 == count) {
                    //状态
                    employeeEntryDto.setEmployeeStatus(statusDemo(mapList.get("value").toString()));
                    if (mapList.size()>1){
                        employeeEntryDto.setEmployeeStatusComment(mapList.get("remark").toString());
                    }
                } else if (8 == count) {
                    //入司前工作年限
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        employeeEntryDto.setWorkYear(Integer.parseInt(mapList.get("value").toString()));
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setWorkYearComment(mapList.get("remark").toString());
                    }
                } else if (9 == count) {
                    //入职级别
                    employeeEntryDto.setJobLevel(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setJobLevelComment(mapList.get("remark").toString());
                    }
                } else if (10 == count) {
                    //招聘HR
                    employeeEntryDto.setHrName(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setHrNameComment(mapList.get("remark").toString());
                    }
                } else if (11 == count) {
                    //维护人员
                    employeeEntryDto.setContactHr(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setContactHrComment(mapList.get("remark").toString());
                    }
                } else if (12 == count) {
                    //电脑状态
                    employeeEntryDto.setComputer(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setComputerComment(mapList.get("remark").toString());
                    }
                } else if (13 == count) {
                    //工商卡号
                    employeeEntryDto.setIcbcCard(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setIcbcCardComment(mapList.get("remark").toString());
                    }
                } else if (14 == count) {
                    //手机号
                    employeeEntryDto.setPhone(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setPhoneComment(mapList.get("remark").toString());
                    }
                } else if (15 == count) {
                    //邮箱号
                    employeeEntryDto.setEmail(mapList.get("value").toString());
                    //原qq改为邮箱，没有邮箱批注
                    /*if (mapList.size()>1){
                        employeeEntryDto.setQqComment(mapList.get("remark").toString());
                    }*/
                } else if (16 == count) {
                    //身份证号
                    if (!("").equals(mapList.get("value").toString())){
                        idCardBirthday=mapList.get("value").toString().substring(6,10)+"/"+mapList.get("value").toString().substring(10,12)+"/"+mapList.get("value").toString().substring(12,14);
                        employeeEntryDto.setIdCardNumber(mapList.get("value").toString());
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setIdCardNumberComment(mapList.get("remark").toString());
                    }
                } else if (17 == count) {
                    //性别
                    employeeEntryDto.setSex(mapList.get("value").toString());
                } else if(18 == count){
                    //民族
                    employeeEntryDto.setNation(mapList.get("value").toString());
                }else if (19 == count) {
                    //身份证地址
                    employeeEntryDto.setIdCardAddress(mapList.get("value").toString());
                } else if (20 == count) {
                    //政治面貌
                        employeeEntryDto.setPoliticsStatus(mapList.get("value").toString());
                    //身份证生日
                   /* if (!("").equals(idCardBirthday)){
                        employeeEntryDto.setIdCardBirthday(sdf.parse(idCardBirthday));
                    }*/
                } else if (21 == count) {
                    //婚否
                    employeeEntryDto.setMaritalStatus(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setMaritalStatusComment(mapList.get("remark").toString());
                    }
                } else if (22 == count) {
                    //阴历/阳历
                    realbirthdayType=mapList.get("value").toString();
                    employeeEntryDto.setRealBirthdayType(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setRealBirthdayTypeComment(mapList.get("remark").toString());
                    }
                } else if (23 == count) {
                    //月
                    m=mapList.get("value").toString();
                } else if (24 == count) {
                    //日
                    d=mapList.get("value").toString();
                } else if (25 == count) {
                    //入职时社保说明
                    employeeEntryDto.setSocialInsuranceBz(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setSocialInsuranceBzComment(mapList.get("remark").toString());
                    }
                } else if (26 == count) {
                    //目前缴纳状态
                    employeeEntryDto.setSocialInsuranceCurr(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setSocialInsuranceCurrComment(mapList.get("remark").toString());
                    }
                } else if (27 == count) {
                    //社保起缴单位
                    employeeEntryDto.setSocialInsurancePlace(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setSocialInsurancePlaceComment(mapList.get("remark").toString());
                    }
                } else if (28 == count) {
                    //社保起缴时间
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        employeeEntryDto.setSocialInsuranceStartTime(mapList.get("value").toString().equals("") ? null : sdf.parse(mapList.get("value").toString()));
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setSocialInsuranceStartTimeComment(mapList.get("remark").toString());
                    }
                } else if (29 == count) {
                    //公积金起缴单位
                    employeeEntryDto.setReserveStartPlace(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setReserveStartPlaceComment(mapList.get("remark").toString());
                    }
                } else if (30 == count) {
                    //公积金起缴时间
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        employeeEntryDto.setReserveStartTime(mapList.get("value").toString().equals("") ? null : sdf.parse(mapList.get("value").toString()));
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setReserveStartTimeComment(mapList.get("remark").toString());
                    }
                } else if (31 == count) {
                    //司龄
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        employeeEntryDto.setCompanyAge(Double.parseDouble(mapList.get("value").toString()));
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setCompanyAgeComment(mapList.get("remark").toString());
                    }
                } else if (32 == count) {
                    //年龄
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        String str = mapList.get("value").toString();
                        str = str.substring(0, str.length() - 2);
                        employeeEntryDto.setAge(Integer.parseInt(str));
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setAgeComment(mapList.get("remark").toString());
                    }
                }else if (33 == count) {
                    //毕业时间
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        employeeEntryDto.setGraduationTime(mapList.get("value").toString().equals("") ? null : sdf.parse(mapList.get("value").toString()));
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setGraduationTimeComment(mapList.get("remark").toString());
                    }
                } else if (34 == count) {
                    //毕业学校
                    employeeEntryDto.setGraduationSchool(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setGraduationSchoolComment(mapList.get("remark").toString());
                    }
                } else if (35 == count) {
                    //专业
                    employeeEntryDto.setSpecialty(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setSpecialtyComment(mapList.get("remark").toString());
                    }
                } else if (36 == count) {
                    //学历
                    employeeEntryDto.setEducationalBackground(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setEducationalBackgroundComment(mapList.get("remark").toString());
                    }
                } else if (37 == count) {
                    //毕业证书编号
                    employeeEntryDto.setDiplomaNo(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setDiplomaNoComment(mapList.get("remark").toString());
                    }
                }else if (38 == count) {
                    //学位证书编号
                    employeeEntryDto.setDegreeCertificateNumber(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setDegreeCertificateNumberComment(mapList.get("remark").toString());
                    }
                }else if (39 == count) {
                    //毕业时间
                    if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                        employeeEntryDto.setSecondGraduationTime(mapList.get("value").toString().equals("") ? null : sdf.parse(mapList.get("value").toString()));
                    }
                    if (mapList.size()>1){
                        employeeEntryDto.setSecondGraduationTimeComment(mapList.get("remark").toString());
                    }
                } else if (40 == count) {
                    //毕业学校
                    employeeEntryDto.setSecondGraduationSchool(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setSecondGraduationSchoolComment(mapList.get("remark").toString());
                    }
                } else if (41 == count) {
                    //专业
                    employeeEntryDto.setSecondSpecialty(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setSecondSpecialtyComment(mapList.get("remark").toString());
                    }
                } else if (42 == count) {
                    //学历
                    employeeEntryDto.setSecondEducationalBackground(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setSecondEducationalBackgroundComment(mapList.get("remark").toString());
                    }
                } else if (43 == count) {
                    //毕业证书编号
                    employeeEntryDto.setSecondDiplomaNo(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setSecondDiplomaComment(mapList.get("remark").toString());
                    }
                }else if (44 == count) {
                    //学位证书编号
                    employeeEntryDto.setSecondDegreeDiplomaNo(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setSecondDegreeDiplomaComment(mapList.get("remark").toString());
                    }
                }else if (45 == count) {
                    //学位证书编号
                    employeeEntryDto.setQualificationCertificate(mapList.get("value").toString());
                    if (mapList.size()>1){
                        employeeEntryDto.setQualificationCertificate(mapList.get("remark").toString());
                    }
                }
                if (!"".equals(m)&&!"".equals(d)&&!"".equals(realbirthdayType)&&!"".equals(idCardBirthday)){
                    employeeEntryDto.setRealBirthday(getRealbirthday(m,d,idCardBirthday,realbirthdayType,sdf));
                }
                /*if (count > 45) {
                    if ((count - 46 == 0) || (count - 46) % 3 == 0) {
                        //项目名称
                        if (count > 46) {
                            projectExcelDto = new Project();
                        }
                        projectExcelDto.setProjectName(mapList.get("value").toString().equals("")?null:mapList.get("value").toString());
                        if (mapList.size()>1){
                            projectExcelDto.setProjectNameComment(mapList.get("remark").toString());
                        }
                        count++;
                        continue;
                    } else if ((count - 47 == 0) || (count - 46) % 3 == 1) {
                        //进项时间
                        if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                            projectExcelDto.setProjectInTime(mapList.get("value").toString().equals("") ? null : sdf.parse(mapList.get("value").toString()));
                        }
                        if (mapList.size()>1){
                            projectExcelDto.setProjectInTimeComment(mapList.get("remark").toString());
                        }
                        count++;
                        continue;
                    } else if ((count - 48 == 0) || (count - 46) % 3 == 2) {
                        //出项时间
                        if(!"".equals(mapList.get("value")) && mapList.get("value")!=null){
                            projectExcelDto.setProjectLeaveTime(mapList.get("value").toString().equals("") ? null : sdf.parse(mapList.get("value").toString()));
                        }
                        if (mapList.size()>1){
                            projectExcelDto.setProjectLeaveTimeComment(mapList.get("remark").toString());
                        }
                        projectExcelDtos.add(projectExcelDto);
                        if (count == listObjectSec.size() - 1) {
                            employeeEntryDto.setProjects(projectExcelDtos);
                        }
                    }
                }*/
                count++;
            }
            if (employeeEntryDto.getJobNumber()!=null){
                employeeEntryDto.setEmployeeType(1);
            }
            sheetList.add(employeeEntryDto);
        }
        return sheetList;
    }

    /**
     * 状态的改变
     *
     * @param status
     * @return
     */
    public static int statusDemo(String status) {
        String EMPLOYEESTATUS1 = "试用";
        String EMPLOYEESTATUS2 = "试用-正式";
        String EMPLOYEESTATUS3 = "正式";
        String EMPLOYEESTATUS4 = "换项";
        String EMPLOYEESTATUS5 = "离职";
        String EMPLOYEESTATUS6 = "待离职";
        String EMPLOYEESTATUS7 = "入职";

        if (EMPLOYEESTATUS1.equals(status)) {
            return 1;
        } else if (EMPLOYEESTATUS2.equals(status)) {
            return 2;
        } else if (EMPLOYEESTATUS3.equals(status)) {
            return 3;
        } else if (EMPLOYEESTATUS4.equals(status)) {
            return 4;
        } else if (EMPLOYEESTATUS5.equals(status)) {
            return 5;
        } else if (EMPLOYEESTATUS6.equals(status)) {
            return 6;
        } else {
            return 7;
        }
    }

    public static Date getRealbirthday(String m,String d,String idCardbirthday,String realbirthdayType,SimpleDateFormat sdf)throws Exception{
        Date realbirthday=null;
        if (Integer.parseInt(m)<10){
            m="0"+m;
        }
        if (Integer.parseInt(d)<10){
            d="0"+d;
        }
        String md=m+d;
        if (!("").equals(idCardbirthday)){
            String idy=idCardbirthday.substring(0,4);
            String idm=idCardbirthday.substring(5,7);
            String idd=idCardbirthday.substring(8,10);
            String idmd=idCardbirthday.substring(5,7)+idCardbirthday.substring(8,10);
            if (!idmd.equals(md)){
                if ("阴历".equals(realbirthdayType)){
                    //阳转阴
                    String strDate= DateUtil.calElement(Integer.parseInt(idy),Integer.parseInt(idm),Integer.parseInt(idd))[0]+"/"
                            +m+"/"
                            +d;
                    realbirthday = sdf.parse(strDate);
                    return realbirthday;
                }else {
                    //阴转阳
                    String strDate= LunarCalendar.lunarToSolar(Integer.parseInt(idy),Integer.parseInt(idm),Integer.parseInt(idd),true)[0]+"/"
                            +m+"/"
                            +d;
                    realbirthday = sdf.parse(strDate);
                    return realbirthday;
                }
            }else {
                if ("阴历".equals(realbirthdayType)){
                    realbirthday = sdf.parse(idCardbirthday);
                    return realbirthday;
                }else {
                    realbirthday = sdf.parse(idCardbirthday);
                    return realbirthday;
                }
            }
        }
        return realbirthday;
    }
}
